#include<string.h>
#include<stdio.h>
#include "lvgl/lvgl.h"
#include "../ui.h"
#include "keyboard.h"
#include "../patient_data.h" 
#include<unistd.h>

// 外部资源声明
extern const lv_img_dsc_t back1;


// 全局组件
static lv_obj_t *input_name;
static lv_obj_t *radio_male;
static lv_obj_t *input_age;
static lv_obj_t *input_id;
static lv_obj_t *input_phone;
static lv_obj_t *radio_female;
static lv_obj_t *confirm_dialog = NULL;
static lv_obj_t *msgbox = NULL;
static lv_obj_t *success_msgbox = NULL;  // 新增：成功提示消息框
static int selected_gender = 0; // 0:男, 1:女

LV_FONT_DECLARE(lv_font_fan_24);

// 函数原型声明
void ui_Screen6_screen_deinit(void);
static void success_msgbox_close_event_handler(lv_event_t *e);  // 新增：成功消息框事件处理

// 创建通用标签
static lv_obj_t *create_label(lv_obj_t *parent, const char *text) {
    if(!parent) return NULL; // 检查父对象有效性
    lv_obj_t *label = lv_label_create(parent);
    lv_label_set_text(label, text);
    lv_obj_set_style_text_font(label, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(label, lv_color_hex(0x555555), 0);
    lv_obj_set_width(label, 100);
    return label;
}

// 性别单选按钮的事件处理函数
static void gender_radio_event_handler(lv_event_t *e) {
    if(!e) return; // 检查事件有效性
    lv_obj_t *clicked_btn = lv_event_get_target(e);
    if(!clicked_btn) return; // 检查目标有效性
    
    // 重置所有按钮为未选中状态
    if(radio_male) { // 检查对象有效性
        lv_obj_set_style_border_color(radio_male, lv_color_hex(0xCCCCCC), 0);
        lv_obj_set_style_border_width(radio_male, 1, 0);
        lv_obj_set_style_bg_color(radio_male, lv_color_white(), 0);
    }
    
    if(radio_female) { // 检查对象有效性
        lv_obj_set_style_border_color(radio_female, lv_color_hex(0xCCCCCC), 0);
        lv_obj_set_style_border_width(radio_female, 1, 0);
        lv_obj_set_style_bg_color(radio_female, lv_color_white(), 0);
    }
    
    // 设置当前点击的按钮为选中状态
    lv_obj_set_style_border_color(clicked_btn, lv_color_hex(0x1E88E5), 0);
    lv_obj_set_style_border_width(clicked_btn, 2, 0);
    lv_obj_set_style_bg_color(clicked_btn, lv_color_hex(0xF0F7FF), 0);
    
    // 更新选中状态变量
    if (clicked_btn == radio_male) {
        selected_gender = 0; // 男
    } else if (clicked_btn == radio_female) {
        selected_gender = 1; // 女
    }
}

// 获取选中性别的函数
static const char* get_selected_gender(void) {
    return selected_gender == 0 ? "男" : "女";
}

// 输入框事件回调函数
static void input_event_cb(lv_event_t *e) {
    if(!e) return; // 检查事件有效性
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *ta = lv_event_get_target(e);
    if(!ta) return; // 检查目标有效性
    
    if (code == LV_EVENT_CLICKED || code == LV_EVENT_FOCUSED) {
        show_keyboard(ta);
        
        if (ta == input_age || ta == input_phone) {
            lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER);
        } 
        else if (ta == input_id) {
            lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER);
        } 
        else if (ta == input_name) {
            lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_TEXT_LOWER);
        }
    }
}

// 创建通用输入框
static lv_obj_t *create_textarea(lv_obj_t *parent, const char *hint_text, bool is_number) {
    if(!parent) return NULL; // 检查父对象有效性
    lv_obj_t *ta = lv_textarea_create(parent);
    lv_textarea_set_placeholder_text(ta, hint_text);
    lv_obj_set_size(ta, 240, 45);
    lv_obj_set_style_radius(ta, 5, 0);
    lv_obj_set_style_bg_color(ta, lv_color_white(), 0);
    lv_obj_set_style_border_color(ta, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_style_border_color(ta, lv_color_hex(0x1E88E5), LV_STATE_FOCUSED);
    lv_obj_set_style_text_font(ta, &lv_font_fan_24, 0);
    
    lv_obj_add_event_cb(ta, input_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ta, input_event_cb, LV_EVENT_FOCUSED, NULL);
    
    if (is_number) {
        lv_textarea_set_accepted_chars(ta, "0123456789Xx");
    }
    return ta;
}

// 提示消息框关闭事件处理
static void msgbox_close_event_handler(lv_event_t *e) {
    if(!e) return; // 检查事件有效性
    
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *msgbox_obj = lv_event_get_current_target(e); // 获取消息框本身
    
    // 确保操作的是正确的消息框
    if(msgbox_obj != msgbox) return;
    
    if (code == LV_EVENT_DELETE || code == LV_EVENT_CLICKED) {
        // 确保消息框指针有效
        if (msgbox) {
            // 解除事件监听避免重复调用
            lv_obj_remove_event_cb(msgbox, msgbox_close_event_handler);
            
            // 保存当前消息框指针并清除全局指针
            lv_obj_t *obj_to_del = msgbox;
            msgbox = NULL;
            
            // 删除消息框
            lv_obj_del(obj_to_del);
        }
        
        // 检查输入框是否有效再操作
        if(input_name && input_age && input_id && input_phone) {
            // 获取或创建输入组
            lv_group_t *group = lv_group_get_default();
            if (!group) {
                group = lv_group_create();
                lv_group_set_default(group);
                
                // 将输入框添加到组中
                lv_group_add_obj(group, input_name);
                lv_group_add_obj(group, input_age);
                lv_group_add_obj(group, input_id);
                lv_group_add_obj(group, input_phone);
            }
            
            // 聚焦到第一个未填写的输入框
            if (strlen(lv_textarea_get_text(input_name)) == 0) {
                lv_group_focus_obj(input_name);
            } else if (strlen(lv_textarea_get_text(input_age)) == 0) {
                lv_group_focus_obj(input_age);
            } else if (strlen(lv_textarea_get_text(input_id)) == 0) {
                lv_group_focus_obj(input_id);
            } else if (strlen(lv_textarea_get_text(input_phone)) == 0) {
                lv_group_focus_obj(input_phone);
            }
        }
        
        // 确保主界面可见
        if (ui_Screen6) {
            lv_obj_clear_flag(ui_Screen6, LV_OBJ_FLAG_HIDDEN);
        }
    }
}

// 新增：成功提示消息框关闭事件处理 - 关闭后跳转到挂号人选择界面
static void success_msgbox_close_event_handler(lv_event_t *e) {
    if(!e) return; // 检查事件有效性
    
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *msgbox_obj = lv_event_get_current_target(e);
    
    // 确保操作的是正确的消息框
    if(msgbox_obj != success_msgbox) return;
    
    if (code == LV_EVENT_DELETE || code == LV_EVENT_CLICKED) {
        // 确保消息框指针有效
        if (success_msgbox) {
            lv_obj_remove_event_cb(success_msgbox, success_msgbox_close_event_handler);
            lv_obj_t *obj_to_del = success_msgbox;
            success_msgbox = NULL;
            lv_obj_del(obj_to_del);
        }
        // 跳转到挂号人选择界面
        ui_Screen5_refresh_list();
        sleep(1);
        
        lv_scr_load(ui_Screen5);  
    }
}

// 确认对话框按钮事件处理
static void dialog_btn_event_handler(lv_event_t *e) {
    if(!e) return; // 检查事件有效性
    
    lv_obj_t *btn = lv_event_get_target(e);
    lv_obj_t *dialog = lv_obj_get_parent(lv_obj_get_parent(btn));
    lv_obj_t *label = lv_obj_get_child(btn, 0);
    
    if(!label || !dialog) return; // 检查对象有效性
    
    if (strcmp(lv_label_get_text(label), "确定") == 0) {
        // 1. 准备要保存的数据
        PatientInfo new_patient;
        memset(&new_patient, 0, sizeof(PatientInfo)); // 初始化结构体
        
        strncpy(new_patient.name, lv_textarea_get_text(input_name), sizeof(new_patient.name) - 1);
        new_patient.gender = selected_gender;
        new_patient.age = atoi(lv_textarea_get_text(input_age)); // 将字符串转换为整数
        strncpy(new_patient.id_card, lv_textarea_get_text(input_id), sizeof(new_patient.id_card) - 1);
        strncpy(new_patient.phone, lv_textarea_get_text(input_phone), sizeof(new_patient.phone) - 1);
        
        // 2. 调用函数将数据保存到管理模块
        int result = patient_add(&new_patient);

        // 3. 关闭确认对话框
        if(confirm_dialog == dialog) {
            lv_obj_del(dialog);
            confirm_dialog = NULL;
        }
        
        // 4. 根据保存结果显示不同的消息
        static const char *btns[] = {"确定", NULL};
        if (result >= 0) {
            // 保存成功
            success_msgbox = lv_msgbox_create(lv_scr_act(), "成功", "挂号人信息添加成功！", btns, false);
        } else {
            // 保存失败（例如，达到最大数量）
            success_msgbox = lv_msgbox_create(lv_scr_act(), "失败", "添加失败，可能已达到最大数量。", btns, false);
        }
        
        if(success_msgbox) {
            lv_obj_set_style_text_font(success_msgbox, &lv_font_fan_24, 0);
            lv_obj_add_event_cb(success_msgbox, success_msgbox_close_event_handler, LV_EVENT_CLICKED, NULL);
            lv_obj_add_event_cb(success_msgbox, success_msgbox_close_event_handler, LV_EVENT_DELETE, NULL);
            lv_obj_set_width(success_msgbox, 300);
            lv_obj_center(success_msgbox);
        } else {
            // 如果消息框创建失败，也直接跳转到选择界面
            
        }
    } else {
        // 安全删除对话框（取消按钮）
        if(confirm_dialog == dialog) {
            lv_obj_del(dialog);
            confirm_dialog = NULL;
        }
    }
}
// 显示确认信息对话框
static void show_confirmation_dialog(void) {
    // 已有对话框则返回
    if (confirm_dialog != NULL || msgbox != NULL || success_msgbox != NULL) {
        return;
    }
    
    // 检查输入框是否存在
    if(!input_name || !input_age || !input_id || !input_phone) {
        return;
    }
    
    // 获取输入的信息
    const char *name = lv_textarea_get_text(input_name);
    const char *age = lv_textarea_get_text(input_age);
    const char *id = lv_textarea_get_text(input_id);
    const char *phone = lv_textarea_get_text(input_phone);
    
    // 验证必填项
    if (strlen(name) == 0 || strlen(age) == 0 || strlen(id) == 0 || strlen(phone) == 0) {
        // 如果已有消息框，先删除
        if (msgbox != NULL) {
            lv_obj_del(msgbox);
            msgbox = NULL;
        }
        
        // 创建消息框按钮
        static const char *btns[] = {"确定", NULL};
        
        // 创建新的提示消息框
        msgbox = lv_msgbox_create(lv_scr_act(), "提示", "请填写完整信息", btns, false);
        if(!msgbox) return;
        
        lv_obj_set_style_text_font(msgbox, &lv_font_fan_24, 0);
        lv_obj_add_event_cb(msgbox, msgbox_close_event_handler, LV_EVENT_CLICKED, NULL);
        lv_obj_add_event_cb(msgbox, msgbox_close_event_handler, LV_EVENT_DELETE, NULL);
        
        lv_obj_set_width(msgbox, 300);
        lv_obj_center(msgbox);
        return;
    }
    
    // 创建确认对话框（其余代码保持不变）
    confirm_dialog = lv_obj_create(lv_scr_act());
    lv_obj_set_size(confirm_dialog, 450, 400);
    lv_obj_center(confirm_dialog);
    lv_obj_set_style_bg_color(confirm_dialog, lv_color_white(), 0);
    lv_obj_set_style_radius(confirm_dialog, 10, 0);
    lv_obj_set_style_shadow_width(confirm_dialog, 10, 0);
    lv_obj_set_style_shadow_color(confirm_dialog, lv_color_hex(0x333333), 0);
    lv_obj_set_style_shadow_opa(confirm_dialog, 50, 0);
    
    // 对话框标题
    lv_obj_t *title = lv_label_create(confirm_dialog);
    lv_label_set_text(title, "确认挂号信息");
    lv_obj_set_style_text_font(title, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(title, lv_color_hex(0x1E88E5), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);
    
    // 信息内容容器
    lv_obj_t *content = lv_obj_create(confirm_dialog);
    lv_obj_set_size(content, 350, 250);
    lv_obj_align(content, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_set_style_bg_opa(content, 0, 0);
    lv_obj_set_layout(content, LV_LAYOUT_GRID);
    
    static lv_coord_t col_dsc[] = {120, 230, LV_GRID_TEMPLATE_LAST};
    static lv_coord_t row_dsc[] = {40, 40, 40, 40, LV_GRID_TEMPLATE_LAST};
    lv_obj_set_style_grid_column_dsc_array(content, col_dsc, 0);
    lv_obj_set_style_grid_row_dsc_array(content, row_dsc, 0);
    
    // 显示信息
    const char *gender = get_selected_gender();
    
    lv_obj_t *l1 = lv_label_create(content);
    lv_label_set_text(l1, "姓名：");
    lv_obj_set_style_text_font(l1, &lv_font_fan_24, 0);
    lv_obj_set_grid_cell(l1, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    
    lv_obj_t *v1 = lv_label_create(content);
    lv_label_set_text(v1, name);
    lv_obj_set_grid_cell(v1, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    
    lv_obj_t *l2 = lv_label_create(content);
    lv_label_set_text(l2, "性别：");
    lv_obj_set_style_text_font(l2, &lv_font_fan_24, 0);
    lv_obj_set_grid_cell(l2, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    
    lv_obj_t *v2 = lv_label_create(content);
    lv_label_set_text(v2, gender);
    lv_obj_set_style_text_font(v2, &lv_font_fan_24, 0);
    lv_obj_set_grid_cell(v2, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    
    lv_obj_t *l3 = lv_label_create(content);
    lv_label_set_text(l3, "年龄：");
    lv_obj_set_style_text_font(l3, &lv_font_fan_24, 0);
    lv_obj_set_grid_cell(l3, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    
    lv_obj_t *v3 = lv_label_create(content);
    lv_label_set_text(v3, age);
    lv_obj_set_grid_cell(v3, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    
    lv_obj_t *l4 = lv_label_create(content);
    lv_label_set_text(l4, "联系电话：");
    lv_obj_set_style_text_font(l4, &lv_font_fan_24, 0);
    lv_obj_set_grid_cell(l4, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 3, 1);
    
    lv_obj_t *v4 = lv_label_create(content);
    lv_label_set_text(v4, phone);
    lv_obj_set_grid_cell(v4, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 3, 1);
    
    // 按钮容器
    lv_obj_t *btn_cont = lv_obj_create(confirm_dialog);
    lv_obj_set_size(btn_cont, 350, 50);
    lv_obj_align(btn_cont, LV_ALIGN_BOTTOM_MID, 0, 50);
    lv_obj_set_layout(btn_cont, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(btn_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_cont, LV_FLEX_ALIGN_SPACE_AROUND, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_bg_opa(btn_cont, 0, 0);
    
    // 取消按钮
    lv_obj_t *cancel_btn = lv_btn_create(btn_cont);
    lv_obj_set_size(cancel_btn, 100, 40);
    lv_obj_set_style_radius(cancel_btn, 5, 0);
    lv_obj_set_style_bg_color(cancel_btn, lv_color_white(), 0);
    lv_obj_set_style_border_color(cancel_btn, lv_color_hex(0x1E88E5), 0);
    lv_obj_add_event_cb(cancel_btn, dialog_btn_event_handler, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *cancel_label = lv_label_create(cancel_btn);
    lv_label_set_text(cancel_label, "取消");
    lv_obj_set_style_text_color(cancel_label, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(cancel_label, &lv_font_fan_24, 0);
    lv_obj_center(cancel_label);
    
    // 确定按钮
    lv_obj_t *ok_btn = lv_btn_create(btn_cont);
    lv_obj_set_size(ok_btn, 100, 40);
    lv_obj_set_style_radius(ok_btn, 5, 0);
    lv_obj_set_style_bg_color(ok_btn, lv_color_hex(0x1E88E5), 0);
    lv_obj_add_event_cb(ok_btn, dialog_btn_event_handler, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *ok_label = lv_label_create(ok_btn);
    lv_label_set_text(ok_label, "确定");
    lv_obj_set_style_text_font(ok_label, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(ok_label, lv_color_white(), 0);
    lv_obj_center(ok_label);
}

// 按钮事件处理函数
static void btn_event_handler(lv_event_t *e) {
    if(!e) return; // 检查事件有效性
    
    lv_obj_t *btn = lv_event_get_target(e);
    lv_obj_t *label = lv_obj_get_child(btn, 0);
    
    if(!label) return; // 检查标签有效性
    
    // 隐藏键盘
    if (keyboard) {
        lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
    }
    
    if (strcmp(lv_label_get_text(label), "确认") == 0) {
        show_confirmation_dialog();
    }
    else if (strcmp(lv_label_get_text(label), "返回") == 0) {
        if (ui_Screen6) {
            // 确保Screen4已初始化
            if(ui_Screen4 == NULL) {
                ui_Screen4_screen_init();
            }
            if(ui_Screen4) {
                lv_scr_load(ui_Screen4);
            }
            lv_obj_del(ui_Screen6);
            ui_Screen6 = NULL;
        }
    }
}

void ui_Screen6_screen_init(void)
{
    force_reset_keyboard();
    // 如果屏幕已存在，先删除
    if (ui_Screen6 != NULL) {
        lv_obj_del(ui_Screen6);
        ui_Screen6 = NULL;
    }
    
    ui_Screen6 = lv_obj_create(NULL);
    if(!ui_Screen6) return; // 检查创建是否成功
    
    lv_obj_clear_flag(ui_Screen6, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_img_src(ui_Screen6, &back1, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_Screen6, lv_color_hex(0xFFFFFF), 0);

    // 标题栏
    lv_obj_t *title_label = lv_label_create(ui_Screen6);
    lv_label_set_text(title_label, "挂号人身份信息登记");
    lv_obj_set_style_text_font(title_label, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(title_label, lv_color_hex(0x1E88E5), 0);
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 100);

    // 表单区域
    lv_obj_t *form_cont = lv_obj_create(ui_Screen6);
    lv_obj_set_size(form_cont, 380, 320);
    lv_obj_align(form_cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_clear_flag(form_cont, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(form_cont, 0, 0);

    // 表单网格布局
    lv_obj_set_layout(form_cont, LV_LAYOUT_GRID);
    static lv_coord_t col_dsc[] = {100, 240, LV_GRID_TEMPLATE_LAST};
    static lv_coord_t row_dsc[] = {50, 50, 50, 50, 50, LV_GRID_TEMPLATE_LAST};
    lv_obj_set_style_grid_column_dsc_array(form_cont, col_dsc, 0);
    lv_obj_set_style_grid_row_dsc_array(form_cont, row_dsc, 0);

    // 姓名输入项
    lv_obj_t *name_label = create_label(form_cont, "姓名：");
    lv_obj_set_grid_cell(name_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1);
    input_name = create_textarea(form_cont, "请输入姓名", false);
    lv_obj_set_grid_cell(input_name, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1);

    // 性别单选项
    lv_obj_t *gender_label = create_label(form_cont, "性别：");
    lv_obj_set_grid_cell(gender_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    
    lv_obj_t *gender_radio_cont = lv_obj_create(form_cont);
    lv_obj_set_size(gender_radio_cont, 240, 45);
    lv_obj_set_style_bg_opa(gender_radio_cont, 0, 0);
    lv_obj_set_layout(gender_radio_cont, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(gender_radio_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(gender_radio_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_grid_cell(gender_radio_cont, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 1, 1);   
    
    // 创建“男”单选按钮
    radio_male = lv_btn_create(gender_radio_cont);
    lv_obj_set_size(radio_male, 80, 40);
    lv_obj_set_style_radius(radio_male, 5, 0);
    lv_obj_add_event_cb(radio_male, gender_radio_event_handler, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *male_label = lv_label_create(radio_male);
    lv_label_set_text(male_label, "男");
    lv_obj_set_style_text_font(male_label, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(male_label, lv_color_hex(0x333333), LV_PART_MAIN);
    lv_obj_center(male_label);
    
    // 默认选中“男”
    lv_obj_set_style_border_color(radio_male, lv_color_hex(0x1E88E5), 0);
    lv_obj_set_style_border_width(radio_male, 2, 0);
    lv_obj_set_style_bg_color(radio_male, lv_color_hex(0xF0F7FF), 0);

    // 创建“女”单选按钮
    radio_female = lv_btn_create(gender_radio_cont);
    lv_obj_set_size(radio_female, 80, 40);
    lv_obj_set_style_radius(radio_female, 5, 0);
    lv_obj_add_event_cb(radio_female, gender_radio_event_handler, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *female_label = lv_label_create(radio_female);
    lv_label_set_text(female_label, "女");
    lv_obj_set_style_text_font(female_label, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(female_label, lv_color_hex(0x333333), LV_PART_MAIN);
    lv_obj_center(female_label);
    
    lv_obj_set_style_border_color(radio_female, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_style_border_width(radio_female, 1, 0);
    lv_obj_set_style_bg_color(radio_female, lv_color_white(), 0);
    
    // 年龄输入项
    lv_obj_t *age_label = create_label(form_cont, "年龄：");
    lv_obj_set_grid_cell(age_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    input_age = create_textarea(form_cont, "请输入年龄", true);
    lv_obj_set_grid_cell(input_age, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    
    // 身份证号输入项
    lv_obj_t *id_label = create_label(form_cont, "身份证号");
    lv_obj_set_grid_cell(id_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 3, 1);
    input_id = create_textarea(form_cont, "请输入18位身份证号", true);
    lv_obj_set_grid_cell(input_id, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 3, 1);

    // 联系电话输入项
    lv_obj_t *phone_label = create_label(form_cont, "联系电话");
    lv_obj_set_grid_cell(phone_label, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_CENTER, 4, 1);
    input_phone = create_textarea(form_cont, "请输入手机号码", true);
    lv_obj_set_grid_cell(input_phone, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_CENTER, 4, 1);

    // 底部功能按钮
    lv_obj_t *btn_cont = lv_obj_create(ui_Screen6);
    lv_obj_set_size(btn_cont, lv_pct(100), 60);
    lv_obj_align(btn_cont, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_layout(btn_cont, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(btn_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_cont, LV_FLEX_ALIGN_SPACE_AROUND, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_bg_opa(btn_cont, 0, 0);

    // 返回按钮
    lv_obj_t *back_btn = lv_btn_create(btn_cont);
    lv_obj_set_size(back_btn, 120, 45);
    lv_obj_set_style_radius(back_btn, 5, 0);
    lv_obj_set_style_bg_color(back_btn, lv_color_white(), 0);
    lv_obj_set_style_border_color(back_btn, lv_color_hex(0xCCCCCC), 0);
    lv_obj_set_style_border_width(back_btn, 1, 0);
    lv_obj_add_event_cb(back_btn, btn_event_handler, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *back_btn_label = lv_label_create(back_btn);
    lv_label_set_text(back_btn_label, "返回");
    lv_obj_set_style_text_font(back_btn_label, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(back_btn_label, lv_color_hex(0x555555), 0);
    lv_obj_center(back_btn_label);

    // 确认按钮
    lv_obj_t *confirm_btn = lv_btn_create(btn_cont);
    lv_obj_set_size(confirm_btn, 120, 45);
    lv_obj_set_style_radius(confirm_btn, 5, 0);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0x1E88E5), 0);
    lv_obj_add_event_cb(confirm_btn, btn_event_handler, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *confirm_btn_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_btn_label, "确认");
    lv_obj_set_style_text_font(confirm_btn_label, &lv_font_fan_24, 0);
    lv_obj_set_style_text_color(confirm_btn_label, lv_color_white(), 0);
    lv_obj_center(confirm_btn_label);
    
    // 加载当前屏幕
    lv_scr_load(ui_Screen6);
}

// 界面切换时调用，重置键盘状态
void ui_Screen6_screen_deinit(void) {
    reset_keyboard();
    
    // 安全删除所有对话框和消息框
    if (confirm_dialog != NULL) {
        lv_obj_del(confirm_dialog);
        confirm_dialog = NULL;
    }
    
    if (msgbox != NULL) {
        lv_obj_del(msgbox);
        msgbox = NULL;
    }
    
    if (success_msgbox != NULL) {  // 新增：清理成功消息框
        lv_obj_del(success_msgbox);
        success_msgbox = NULL;
    }
    
    // 清除输入框指针
    input_name = NULL;
    radio_male = NULL;
    input_age = NULL;
    input_id = NULL;
    input_phone = NULL;
    radio_female = NULL;
    
    // 删除屏幕对象
    if(ui_Screen6) {
        lv_obj_del(ui_Screen6);
        ui_Screen6 = NULL;
    }
}
